param (
    [string]$User="",
    [string]$User_Pwd=""
)

<#
    测试Exchange连通性
#>

$DATA_BACKUP_AGENT_HOME = (Get-ChildItem env:DATA_BACKUP_AGENT_HOME).Value
$Location = $DATA_BACKUP_AGENT_HOME + "\DataBackup\ProtectClient\Plugins\GeneralDBPlugin\bin\applications\exchange\ps1_scripts"
Set-Location -Path $Location
[string]$common_file_path = (Get-Location).Path + "\Common.ps1"
[string]$exchangeServiceName = "MSExchangeIS"
[string]$PSSession_Name = "CheckAppConnection"

# 引用Common.ps1
. $common_file_path

# 检查MSExchangeIS服务是否正在运行
# 若正在运行的服务列表中存在名为MSExchangeIS的，返回true
# 用于获取正在运行的服务器列表
$serviceDetail = Get-service $exchangeServiceName
# 不存在或者状态不正常
if (!$? -or ([string]$serviceDetail.Status -ne "Running")) {
    $Script:Result["ReturnCode"] = "0x5E025E27"
    $Script:Result["Output"] = $exchangeServiceName
    return $Result | ConvertTo-Json -Compress -Depth 7
}

# 建立连接
if (!(NewConnection $User $User_Pwd $PSSession_Name)) {
    return $Result | ConvertTo-Json -Compress -Depth 7
}

$Script:Result["ReturnCode"] = "0"
return $Result | ConvertTo-Json -Compress -Depth 7